<?php
namespace app\admin\model;

use think\facade\Db;
use think\Model;
use think\facade\Request;
use think\model\concern\SoftDelete;


class Goods extends Model
{
    use SoftDelete;
    protected $pk = 'goods_id';
    protected $deleteTime = 'delete_time';
    public static function getGoodList($where,$data,$order)
    {
        $list = Goods::alias('g')
            ->field('g.goods_id,g.goods_name,g.default_image,g.recommended,g.if_show,g.add_time,g.price,g.images')
            ->field('c.cate_name')
            ->field('b.brand_name')
            ->join('gcategory c','c.cate_id = g.cate_id')
            ->join('brand b','b.brand_id = g.brand')
            ->where($where)
            ->order($order)
            ->paginate(
                [
                    'list_rows'=>5,
                    'query'=>$data,
                ]
            );
        return $list;
    }
    //获取器显示前端图片
    public static function getImagesNextAttr($v,$data)
    {
        if ($data['images']==NULL){
            return "<img src='/static/data/default.png' width='70' height='70' alt='' >";
        }else{
            return "<img src='/static/{$data['images']}' width='70' height='70' alt='' >";
        }
    }
    //获取器显示推荐图片
    public static function getRecommendedNextAttr($v,$data)
    {
        if ($data['recommended']==1){
            return '<img src="/static/data/dui.jpg" alt=""  width="20px" height="20px">';
        }else{
            return '<img src="/static/data/X.jpg" alt=""  width="20px" height="20px">';
        }
    }
    //获取器显示下架图片
    public static function getIfShowNextAttr($v,$data)
    {
        if ($data['if_show']==1){
            return '<img src="/static/data/dui.jpg" alt=""  width="20px" height="20px">';
        }else{
            return '<img src="/static/data/X.jpg" alt=""  width="20px" height="20px">';
        }
    }
    public function getdefaultImageAttr($v)
    {
        if (!empty($v)){
            return explode(',',$v);
        }
    }

    //添加到数据库
    public static function addGoodsInfo($add)
    {
        try{
            $res = Goods::strict(true)->save($add);
            $msg = '成功';
        }catch (\Exception $e){
            $res = false;
            $msg = $e->getMessage();
        }
        return $res;
    }
    //修改商品表
    public static function editGoodsInfo($get)
    {
//        if (Request::isPost()){
//            try{
//                $res = Goods::update($edit);
//                $msg = '成功';
//            }catch (\Exception $e){
//                $res = false;
//                $msg = $e->getMessage();
//            }
//            return [$res,$msg];
//        }

        $list = Goods::where('goods_id','=',$get['id'])->find()->toArray();
        return $list;
    }
    //软删除商品表
    public static function delGoodsInfo($get)
    {
        try{
            $res = Goods::destroy($get['id']);
            $msg = '删除成功';
        }catch (\Exception $e){
            $res = false;
            $msg = '删除失败';
        }
        return [$res,$msg];
    }
    public static function delsGoodsInfo($get)
    {
        $id = explode(',',$get['id']);
        $res = Goods::destroy($id);
        return $res;
    }
    //回收站
    public static function huishouList()
    {
        //查询软删除的数据
        $list = Goods::onlyTrashed()
            ->alias('g')
            ->field('g.goods_id,g.goods_name,g.default_image,g.recommended,g.if_show,g.add_time,g.price,g.images')
            ->field('c.cate_name')
            ->field('b.brand_name')
            ->join('gcategory c','c.cate_id = g.cate_id')
            ->join('brand b','b.brand_id = g.brand')
            ->paginate(
                [
                    'list_rows'=>5,
                ]
            );

        return $list;
    }
    //恢复软删数据
    public static function recoveryInfo($get)
    {
        try{
            $res = Goods::onlyTrashed()->find($get['id']);
            $res->restore();
            $msg = '恢复成功';
        }catch (\Exception $e){
            $res = false;
            $msg = '恢复失败';
        }
        return [$res,$msg];
    }
    //真删除软删除数据
    public static function huishouDel($get)
    {
        try{
            $res = Goods::destroy($get['id'],true);
            $msg = "删除成功";
        }catch (\Exception $e){
            $res = false;
            $msg = "删除失败";
        }
        return [$res,$msg];
    }





}